package com.yl.ylnews;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.GestureDetector;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.yl.ylnews.global.GlobalConstants;
import com.yl.ylnews.view.BackPressWebView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2016/10/20 0020.
 */

public class NewsWebActivity extends Activity implements View.OnClickListener {

    @BindView(R.id.btn_web_menu)
    ImageButton btnWebMenu;
    @BindView(R.id.tv_web_tittle)
    TextView tvWebTittle;
    @BindView(R.id.btn_web_share)
    ImageButton btnWebShare;
    @BindView(R.id.btn_web_font)
    ImageButton btnWebFont;
    @BindView(R.id.number_progress_bar)
    NumberProgressBar numberProgressBar;
    @BindView(R.id.wv_news_tab)
    BackPressWebView wvNewsTab;


    private WebSettings settings;
    private int mTempWhich;
    private String url;
    private GestureDetector gestureDetector;
    private int scrollSize;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_news_web);
        Log.d(TAG, "onCreate: 跳转到Web View界面");
        ButterKnife.bind(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("start web view");
        wvNewsTab.loadUrl(url);
        Log.d(TAG, "onCreate: url:" + url);
        settings = wvNewsTab.getSettings();
        settings.setUseWideViewPort(true);//支持双击缩放
        wvNewsTab.setWebChromeClient(new MyWebChromeClient());
        btnWebMenu.setOnClickListener(this);
        btnWebShare.setOnClickListener(this);
        btnWebFont.setOnClickListener(this);

        Log.d(TAG, "onCreate: "+ GlobalConstants.LOCAL_CACHE_PATH);
        scrollSize = getWindowManager().getDefaultDisplay().getWidth();
    }
    /*@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        wvNewsTab.setOnTouchListener(new View.OnTouchListener() {
            int startX = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        int endX = (int) event.getX();

                        if(endX>startX && wvNewsTab.canGoBack() && endX-startX>scrollSize){
                            Log.d(TAG, "onTouch: goBack");
                            wvNewsTab.goBack();
                        }else if(endX<startX &&wvNewsTab.canGoForward() && startX-endX>scrollSize){
                            wvNewsTab.goForward();
                            Log.d(TAG, "onTouch: goForward");
                        }
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        return super.dispatchTouchEvent(ev);
    }*/



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_web_menu:
                onBackPressed();
                break;
            case R.id.btn_web_share:
                shareWeb();
                break;
            case R.id.btn_web_font:
                selectWebFont();
                break;
        }
    }

    private void shareWeb() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://oenmdzxge.bkt.clouddn.com/icon_150_meitu_3.png");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("ShareSDK");
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);

// 启动分享GUI
        oks.show(this);
    }




    private class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            numberProgressBar.setProgress(newProgress);
            if (newProgress == 100) {
                numberProgressBar.setVisibility(View.GONE);
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            tvWebTittle.setText("头条新闻^_-");
        }
    }

    int currentItem = 2;

    private void selectWebFont() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("字体设置");
        String[] items = new String[]{"超大号字体", "大号字体", "正常字体", "小号字体",
                "超小号字体"};

        builder.setSingleChoiceItems(items, currentItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mTempWhich = which;
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (mTempWhich) {
                    case 0:
                        // 超大字体
                        settings.setTextSize(WebSettings.TextSize.LARGEST);
                        // settings.setTextZoom(22);
                        break;
                    case 1:
                        // 大字体
                        settings.setTextSize(WebSettings.TextSize.LARGER);
                        break;
                    case 2:
                        // 正常字体
                        settings.setTextSize(WebSettings.TextSize.NORMAL);
                        break;
                    case 3:
                        // 小字体
                        settings.setTextSize(WebSettings.TextSize.SMALLER);
                        break;
                    case 4:
                        // 超小字体
                        settings.setTextSize(WebSettings.TextSize.SMALLEST);
                        break;

                    default:
                        break;
                }
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }
}
